﻿/*******************************************************************************
#                    QuestToExpertsBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: Diddy
#
#  History List:
#  V1.0    Created by Onions@2009-8-18 20:07:18
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
#endregion

namespace ExpertMIS.Brokers
{
    public class QuestToExpertsBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateQuestToExpertsEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateQuestToExpertsEntity", entity);
        }

        private object InsertQuestToExpertsEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertQuestToExpertsEntity", entity);
        }

        private object DeleteQuestToExpertsEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteQuestToExpertsEntity", entity);
        }

        private void RemoveQuestionToExperts(string QuestionOID)
        {
            SqlMap.Delete("DeleteQuestToExperts", QuestionOID);
        }
        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectQuestToExpertsEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            if (entity.EntityState == EntityState.New)
            {
                InsertQuestToExpertsEntity(entity);
                entity.EntityState = EntityState.Old;
            }
            else
            {
                UpdateQuestToExpertsEntity(entity);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            QuestToExpertsEntity entity = new QuestToExpertsEntity();

            // initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteQuestToExpertsEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Methods

        public void UpdateQuestionToExperts(string QuestionOID, List<QuestToExpertsEntity> questToExpertList)
        {
            try
            {
                SqlMap.BeginTransaction();

                RemoveQuestionToExperts(QuestionOID);
                foreach (QuestToExpertsEntity entity in questToExpertList)
                {
                    InsertQuestToExpertsEntity(entity);
                }

                SqlMap.CommitTransaction();
            }
            catch (Exception ex)
            {
                SqlMap.RollBackTransaction();
                throw ex;
            }
        }

        #endregion

        public IList<QuestToExpertsEntity> GetQuestToExpertsByQuestionOID(string QuestionOID)
        {
            return SqlMap.QueryForList<QuestToExpertsEntity>("SelectQuestToExpertsByQuestionOID", QuestionOID);
        }
    }
}